package com.bill.robot.koalacam;

import java.io.File;
import java.nio.charset.Charset;

import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Pad 接口
 * @author litong
 * 2016年7月5日
 */
@SuppressWarnings("deprecation")
public class Pad {

  /**
  * 登录
  * @param url
  * @param username
  * @param password
  * @param pad_id
  * @throws Exception
  */
  public static void login(String url, String username, String password, String pad_id) throws Exception {
    System.out.println("Start POST /pad/login 登录...");

    // 创建HttpClient 不需要设置CookieStore
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost request = new HttpPost(url);

    // 设置Content-Type
    request.setHeader("Content-Type", "application/json");

    JSONObject json = new JSONObject();
    json.put("username", username);
    json.put("password", password);
    json.put("device_type", "2");// 固定值
    json.put("pad_id", pad_id);
    request.setEntity(new StringEntity(json.toString(), "UTF-8"));

    CloseableHttpResponse response = httpclient.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

    // 解析json 得到screen_token 用作主机上 1:1 和 1:N接口的参数
    JSONObject resp = new JSONObject(responseBody);
    int result = resp.optInt("code", -1);
    if (result != 0) {
      System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
    } else {
      JSONObject data = resp.getJSONObject("data");
      JSONArray boxes = data.getJSONArray("boxes");
      for (int i = 0; i < boxes.length(); i++) {
        JSONObject box = (JSONObject) boxes.get(i);
        System.out.println("box_address:" + box.getString("box_address"));
        System.out.println("box_token:" + box.getString("box_token"));
      }
      System.out.println("screen_token:" + data.getString("screen_token"));
    }
  }

  /**    
   * 添加访客
   * @param cookieStore cookie值
   * @param url 
   * @param name 访客姓名
   * @param photo 用户识别的底库照片
   * @throws Exception
   */
  public static void addVisitor(CookieStore cookieStore, String url, String name, String photo) throws Exception {
    System.out.println("Start POST /pad/add-visitor 添加访客..." + url);

    // 自定义HttpClient 设置CookieStore
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    HttpPost request = new HttpPost(url);

    // 设置访客名字和底库照片
    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("photo", new FileBody(new File(photo)));
    reqEntity.addPart("name", new StringBody(name, Charset.forName("UTF-8")));
    request.setEntity(reqEntity);

    CloseableHttpResponse response = httpclient.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

    // 解析JSON数据
    JSONObject resp = new JSONObject(responseBody);
    int result = resp.optInt("code", -1);
    if (result != 0) {
      System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
    } else {
      System.out.println("id:" + resp.getJSONObject("data").getString("id"));
    }
  }

  /**    
   * 设置pad信息
   * @param cookieStore cookie值
   * @param url
   * @param pad_id pad_id
   * @param box_token 当前连接的主机box_token
   * @throws Exception
   */
  public static void setInfo(CookieStore cookieStore, String url, String pad_id, String box_token) throws Exception {
    System.out.println("Start PUT /pad/set-info 设置pad信息...");

    // 自定义HttpClient 设置CookieStore
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    HttpPut request = new HttpPut(url);

    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("pad_id", new StringBody(pad_id));
    reqEntity.addPart("box_token", new StringBody(box_token));
    request.setEntity(reqEntity);

    CloseableHttpResponse response = httpclient.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

    // 解析JSON数据
    JSONObject resp = new JSONObject(responseBody);
    int result = resp.optInt("code", -1);
    if (result != 0) {
      System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
    } else {
      System.out.println(resp.toString());
    }
  }
}